package mysql

import "gorm.io/gorm"

type Order struct {
	gorm.Model
	UserId    int     `gorm:"type:int;comment:用户id"`
	TakeoutId int     `gorm:"type:int;comment:餐品id"`
	OrderSn   string  `gorm:"type:varchar(36);comment:订单号"`
	Name      string  `gorm:"type:varchar(20);comment:餐品名称"`
	Price     float64 `gorm:"type:decimal(10,2);comment:餐品价格"`
	BuyNum    int     `gorm:"type:int;comment:购买的数量"`
	PayPrice  float64 `gorm:"type:decimal(10,2);comment:实付价格"`
	PayTypes  int     `gorm:"type:int;default:1;comment:支付方式:1-支付宝，2-微信，3-银联"`
	Coupon    int     `gorm:"type:int;default:1;comment:是否选择优惠卷:1-否，2-是"`
	Notes     string  `gorm:"type:varchar(100);comment:备注"`
	PayStatus int     `gorm:"type:int;default:1;comment:支付状态:1-未支付，2-支付失败，取消订单，3-支付成功"`
}

func (Order) TableName() string {
	return "order"
}

func (o *Order) Created() error {
	return DB.Create(&o).Error
}

func (o *Order) UpdatedStatus(orderSn string, status int) error {
	return DB.Where("order_sn=?", orderSn).Update("pay_status", status).Error
}

func (o *Order) Find() (result []Order, err error) {
	err = DB.Raw("SELECT * FROM `order` ORDER BY order.pay_status=3").Scan(&result).Error
	if err != nil {
		return nil, err
	}
	return result, nil
}
